 /*
 * @description: 客服管理/学生客服
 * @Author: along
 * @Date: 2020-09-27
 * @Last Modified by: along
 * @Last Modified time: 2020-09-27
 */
<template>
    <div class="customStudent">
        <!-- filter -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">商家店铺</p>
                    <el-input
                        v-model="formData.shop_title"
                        placeholder="请输入店铺名称"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">学生姓名</p>
                    <el-input
                        v-model="formData.staff_name"
                        placeholder="请输入姓名"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">学生状态</p>
                    <el-select
                        v-model="formData.staff_status"
                        clearable
                        filterable
                        placeholder="请选择状态"
                        size="small"
                        @change="getTableData()"
                    >
                        <el-option
                            v-for="item in formData.staffStatus"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="formData.dialogVisible = true"
                    >
                        <i
                            class="iconfont el-icon-hand-iconjia"
                            :style="{fontSize: '11px'}"
                        />
                        添加客服
                    </el-button>
                </div>
            </div>
        </div>

        <!-- 校内兼职客服 -->
        <el-table
            :data="dataList"
            style="width: 100%;"
            height="100%"
            border
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="true_name"
                label="学生姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="school"
                label="学校名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="phone"
                label="联系方式"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="shop_title"
                label="工作商家"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                label="工作时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <p
                        @click="fnCheckWordDate(scope.row)"
                        class="cursor-font"
                    >查看</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="valid_time"
                label="账号有效期截止"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    {{ scope.row.valid_second * 1000 | restFormatter }}
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <p
                        v-if="scope.row.status == 1"
                    ><span class="enable" />已启用</p>
                    <p
                        v-if="scope.row.status == 0"
                        :style="{color: '#999999'}"
                    ><span class="out_service" />已停用</p>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
                :width="170"
            >
                <template slot-scope="scope">
                    <div class="table-button">
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="fnCheckData(scope.row)"
                        >
                            查看资料
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="fnSetDate(scope.row)"
                        >
                            设置排期
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="pagination.now_page"
                :total="pagination.total_count"
                :page-size="pagination.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 绑定客服 -->
        <el-dialog
            title="添加客服"
            :visible.sync="formData.dialogVisible"
            :close-on-click-modal="false"
            width="640px"
            @close="fnCloseDialog()"
        >
            <div :style="{marginBottom: '20px'}">
                商家店铺
                <el-select
                    v-model="formData.shop"
                    filterable
                    remote
                    reserve-keyword
                    placeholder="请输入关键词搜索商家店铺"
                    :remote-method="remoteMethod"
                    :loading="loading"
                    size="small"
                    :style="{marginLeft: '10px', width: '378px'}"
                >
                    <el-option
                        v-for="item in formData.suggestList"
                        :key="item.shop_no"
                        :label="item.shop_title"
                        :value="item.shop_no"
                    />
                </el-select>
            </div>
            <div class="_kf">
                学生客服
                <el-input
                    v-model="formData.keyword"
                    size="small"
                    @keyup.native.enter="fnBindSearch()"
                    placeholder="请输入学生手机号或ID"
                    :style="{margin: '0 15px'}"
                />
                <el-button
                    type="primary"
                    size="small"
                    @click="fnBindSearch()"
                >
                    搜索
                </el-button>
            </div>
            <el-table
                :data="formData.tableDataBind"
                height="100%"
                style="width: 100%;max-height: 450px"
                border
                v-if="formData.tableDataBind.length > 0"
            >
                <el-table-column
                    label="用户姓名"
                    prop="true_name"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    label="电话号码"
                    prop="phone"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    label="学校名称"
                    prop="school_name"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    label="操作"
                    prop="school_name"
                    :formatter="emptyFormatter"
                    align="center"
                >
                    <template slot-scope="scope">
                        <span
                            v-if="!scope.row.flag"
                            class="cursor-font"
                            @click="fnChangeKf(scope.row, true)"
                        >选择</span>
                        <span
                            v-if="scope.row.flag"
                            class="cursor-font"
                            @click="fnChangeKf(scope.row, false)"
                        >取消选择</span>
                    </template>
                </el-table-column>
            </el-table>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="fnCloseDialog()"
                    size="medium"
                    :style="{width: '120px', marginRight: '40px'}"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="medium"
                    @click="fnSaveKf()"
                    :style="{width: '120px', marginRight: '40px'}"
                >保 存</el-button>
            </span>
        </el-dialog>

        <!-- 查看排期时间 -->
        <el-dialog
            title="排期时间"
            :visible.sync="dialogVisible"
            :close-on-click-modal="false"
            width="510px"
        >
            <el-table
                :data="dateList"
                style="width: 100%;max-height:450px"
                height="100%"
                :show-header="false"
                border
            >
                <el-table-column
                    type="index"
                    width="80"
                    label="序号"
                    align="center"
                />
                <el-table-column
                    prop="start_time"
                    label="排期时间"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }} ~ {{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script>

const LIST = {
    shop_title: '',//商家店铺,
    staff_name: '',//员工姓名
    staff_status: 99,//员工状态
    staffStatus: [{value: 99, label: '全部'},{value: 1, label: '已启用'},{value: 0, label: '已停用'}],
    dialogVisible: false,
    keyword: '',//搜索学生客服
    tableDataBind: [],//学生客服列表
    shop: '',//店铺编号
    suggestList: [],//商家店铺数据列表
};

export default {
    name: 'CustomStudent',
    data () {
        return {
            formData: {
                ...LIST
            },
            //校内兼职客服
            pagination: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            dataList: [],
            authResult: {},
            //查看排期
            dialogVisible: false,
            dateList: [],
            loading: false
        };
    },
    watch: {
        formData: {
            deep: true,
            handler: function (val) {
                //
            }
        }
    },
    mounted () {
        this.init();
    },
    methods: {
        /**
         * @description 页面数据初始化
         */
        init () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{ title: "客服系统"},{title: '客服管理'},{title: '学生客服'}]);
            this.authResult = this.$route.meta.authResult;
            this.getTableData(1);
            this.getSuggest();
        },

        /**
         * @description 获取校内兼职客服列表
         */
        getTableData (page) {
            this.$post('/student/customer_service_student%5Cget_list', {
                page_id: page || this.pagination.now_page,
                shop_title: this.formData.shop_title,
                student_true_name: this.formData.staff_name,
                status: this.formData.staff_status
            }, resp => {
                if (resp.code == 1) {
                    this.dataList = resp.data.list;
                    this.pagination.now_page = resp.data.page.now_page;
                    this.pagination.total_count = resp.data.page.total_count;
                    this.pagination.page_size = resp.data.page.page_size;
                }
            });
        },

        remoteMethod (query) {
            if (query !== '') {
                this.loading = true;
                setTimeout(() => {
                    this.loading = false;
                    this.getSuggest(query);
                }, 200);
            }
        },

        /**
         * @description 获取商家店铺数据列表
         */
        getSuggest (keyword) {
            this.$post('/student/shop%5Csuggest', {
                keyword: keyword
            }, resp => {
                if (resp.code == 1) {
                    this.$set(this.formData, 'suggestList', resp.data.list);
                }
            });
        },

        /**
         * @description 查看掌大客服排期时间
         */
        fnCheckWordDate (row) {
            const start_time = this.getDate(0) / 1000;
            const end_time = this.getYear() / 1000;

            this.$post('/student/customer_service_student%5Cget_schedule',{
                shop_no: row.shop_no,
                kf_no: row.kf_no,
                start_time: start_time,
                end_time: end_time
            },resp=>{
                if (resp.code == 1) {
                    this.dateList = resp.data.list;
                    this.dialogVisible = !this.dialogVisible;
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 查看资料
         */
        fnCheckData (row) {
            this.$router.push({
                name: 'customStudentInfo',
                query: {
                    no: JSON.stringify({
                        ...row
                    })
                }
            });
        },

        /**
         * @description 激活已过期学生
         */
        fnActive (row) {
            this.$confirm('是否要激活此学生客服', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('',{
                    //
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '激活成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 设置排期
         */
        fnSetDate (row) {
            this.$router.push({
                name: 'customStudentDate',
                query: {
                    no: JSON.stringify({
                        ...row
                    })
                }
            });
        },

        /**
         * @description 保存客服
         */
        fnSaveKf () {
            if(!this.formData.shop) {
                this.$notify({
                    title: '提示',
                    message: '请先选择商家店铺',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }

            let kf_student = [];

            this.formData.tableDataBind.map(item=>{
                if(item.flag) {
                    kf_student.push(item.student_no);
                }
            });

            if(!kf_student.length) {
                this.$notify({
                    title: '提示',
                    message: '请先选择客服,不可以为空',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }

            console.log('kf_student', kf_student.join());

            this.$post('/student/customer_service_student%5Cadd',{
                student_no: kf_student.join(),
                shop_no: this.formData.shop
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.formData.dialogVisible = !this.formData.dialogVisible;
                    this.$set(this.formData, 'tableDataBind', []);
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 选择/取消选择/客服
         */
        fnChangeKf (row, flag) {
            this.$set(row, 'flag', flag);
        },

        /**
         * @description 搜索学生客服
         */
        fnBindSearch () {
            if(this.formData.keyword.length !== 11) {
                this.$notify({
                    title: '提示',
                    message: '请输入正确的手机号',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$post('/student/student_user%5Cget_list_suggest',{
                keyword: this.formData.keyword
            },resp=>{
                if (resp.code == 1) {
                    if (resp.data.list.length == 0) {
                        this.$notify({
                            title: '提示',
                            message: '未搜索到该用户!',
                            type: 'warning',
                            duration: 2000
                        });
                        this.$set(this.formData, 'keyword', '');
                        return;
                    }
                    this.$set(this.formData, 'tableDataBind', resp.data.list);
                    this.$set(this.formData, 'keyword', '');
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 关闭弹窗
         */
        fnCloseDialog () {
            this.$set(this.formData, 'dialogVisible', false);
            this.$set(this.formData, 'tableDataBind', []);
        },

        /**
         * @description 获取指定日期时间戳
         */
        getDate (day) {
            let tempDate = new Date();
            let retult_milliseconds = tempDate.getTime() + 1000 * 60 * 60 * 24 * day;

            tempDate.setTime(retult_milliseconds);

            let result_Year = tempDate.getFullYear(),
                result_Month = (tempDate.getMonth() + 1).toString(),
                result_Date = tempDate.getDate().toString();

            if (result_Month.length === 1) {
                result_Month = "0" + result_Month;
            }

            if (result_Date.length === 1) {
                result_Date = "0" + result_Date;
            }

            let date = result_Year + "-" + result_Month + "-" + result_Date + " " + "00:00:00";

            return new Date(date).getTime();
        },

        /**
         * @description 获取一年后当天的时间戳
         */
        getYear () {
            let year = new Date().getFullYear() + 1;
            let month = new Date().getMonth() + 1;
            let date = new Date().getDate();

            month = month < 10 ? 0 + month : month;
            date = date < 10 ? 0 + date : date;

            return new Date(year+'-'+month +'-'+date + ' ' + '00:00:00').getTime();
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
.customStudent {
    width: 100%;
    height: 100%;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    position: relative;
    .wrap-title {
        color: #333333;
        font-weight: 500;
        font-size: 16px;
        display: flex;
        cursor: pointer;
        margin-bottom: 20px;
        .el-icon-hand-bangzhu {
            font-size: 15px;
            color: #666666;
            margin-left: 8px;
        }
    }
    .header-nav {
        position: relative;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        margin-top: 9px;
        .header-left, .header-right {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
        }
        .header-new-box {
            display: flex;
            margin-right: 15px;
            align-items: center;
            color: #333333;
            margin-bottom: 10px;
            white-space: nowrap;
            &-title {
                margin-right: 16px;
                white-space: nowrap;
            }
        }
    }
    .cursor-font {
        color: #20A0FF;
        cursor: pointer;
    }
    .enable,.out_service,.overdue {
        display: inline-block;
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: #4CD964;
        margin-right: 6px;
        position: relative;
        top: -2px;
    }
    .out_service {
        background: #FF3030;
    }
    .overdue {
        background: #A6A6A6;
    }
    /* 分页 */
    .pagination-wrapper {
        padding-top: 10px;
        padding-bottom: 10px;
        .el-pagination {
            float: right;
            padding: 0;
            background-color: transparent;
        }
    }
    .table-button {
        display: flex;
        align-items: center;
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
    ._kf {
        height: 32px;
        width: 100%;
        display: flex;
        align-items: center;
        white-space: nowrap;
        margin-bottom: 24px;
    }
}
</style>

<style lang="less">
.customManagement {
    .el-tabs__nav {
        .el-tabs__item {
            color: #999999;
        }
        .is-active {
            font-weight: 500;
            color: #20A0FF;
        }
    }
    .el-tabs__nav-wrap::after {
        background-color: #EBEEF5;
    }
    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
    .table-button .el-button {
        padding: 4px 5px;
        margin-left: 0;
    }
    .el-dialog__header {
        padding-top: 10px;
        box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    .el-dialog__body {
        padding: 30px 60px;
    }
    .el-table__body-wrapper {
        &::-webkit-scrollbar {
            width: 4px;
            border-radius: 3px;
        }
    }
}
.reception-poper {
    color: #666666;
    line-height: 20px;
    font-size: 13px;
    padding: 16px;
}
</style>